package com.army.stock.service.impl;

import com.alibaba.fastjson.JSON;
import com.alibaba.fastjson.JSONArray;
import com.alibaba.fastjson.JSONObject;
import com.army.stock.dao.ArmyInstorageOrderDao;
import com.army.stock.dao.ArmyMonthplanDao;
import com.army.stock.domain.ArmyInstorageOrder;
import com.army.stock.service.ArmyInstorageOrderService;
import com.army.stock.service.ArmySupplierService;
import com.javalife.common.domain.page.PaginatedArrayList;
import com.javalife.common.util.date.DateFormatUtils;
import org.apache.commons.collections4.CollectionUtils;
import org.apache.commons.lang.StringUtils;

import java.math.BigDecimal;
import java.util.Date;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

/**
 * Created by zhangyong on 15/3/7.
 */
public class ArmyInstorageOrderServiceImpl implements ArmyInstorageOrderService {

    private ArmyInstorageOrderDao armyInstorageOrderDao;

    private ArmyMonthplanDao armyMonthplanDao;

    public void setArmyInstorageOrderDao(ArmyInstorageOrderDao armyInstorageOrderDao) {
        this.armyInstorageOrderDao = armyInstorageOrderDao;
    }

    public void setArmySupplierService(ArmySupplierService armySupplierService) {
        this.armySupplierService = armySupplierService;
    }

    public void setArmyMonthplanDao(ArmyMonthplanDao armyMonthplanDao) {
        this.armyMonthplanDao = armyMonthplanDao;
    }

    private ArmySupplierService armySupplierService;

    public PaginatedArrayList getInstorageOrderPageInfo(int currentPage, int pageSize, Map<String, Object> map) {
        int startNum = (currentPage - 1) * pageSize;
        map.put("start", startNum);
        map.put("pageSize", pageSize);
        Integer count = armyInstorageOrderDao.getCountInstorageOrder(map);
        List<ArmyInstorageOrder> instorageOrders = armyInstorageOrderDao.gettInstorageOrderList(map);
        if(CollectionUtils.isNotEmpty(instorageOrders)){
            for(ArmyInstorageOrder armyInstorageOrder : instorageOrders){
                armyInstorageOrder.setSupplierName(
                        armySupplierService.getArmySupplierByIdFromCache(
                        Long.valueOf(armyInstorageOrder.getSupplierId())).getSupplierName());
            }
        }
        PaginatedArrayList<ArmyInstorageOrder> paginatedArrayList = new PaginatedArrayList<ArmyInstorageOrder>(currentPage, pageSize);
        paginatedArrayList.setTotalItem(count);
        paginatedArrayList.addAll(instorageOrders);
        return paginatedArrayList;
    }

    public synchronized void addInstrageOrder(String instorageJson) {
        JSONObject jsonObject = JSON.parseObject(instorageJson);
        String code = jsonObject.getString("code");
        String priceSum = jsonObject.getString("priceSum");
        String checkMan = jsonObject.getString("checkMan");
        String sendMan = jsonObject.getString("sendMan");
        String buyMan = jsonObject.getString("buyMan");
        String supplierId = jsonObject.getString("supplierId");
        String source = jsonObject.getString("source");
        JSONArray items = jsonObject.getJSONArray("items");
        Map map = new HashMap();
        map.put("code", code);
        map.put("amount", priceSum);
        map.put("checkMan", checkMan);
        map.put("supplierId", supplierId);
        map.put("sendMan", sendMan);
        map.put("buyMan", buyMan);
        map.put("instorageDate", DateFormatUtils.format(new Date() ,"yyyy-MM-dd"));
        map.put("source", source == null ? "0" : source);

        armyInstorageOrderDao.insertInstorageOrder(map);
        Long instorageOrderId = (Long) map.get("id");
        for (int i = 0; i < items.size(); i++) {
            JSONObject item = items.getJSONObject(i);
            String goodid = item.getString("goodid");
            String goodnum = item.getString("goodnum");
            String goodprice = item.getString("goodprice");
            String monthPlanId = item.getString("monthplanid");
            Map itemMap = new HashMap();
            itemMap.put("goodId", goodid);
            itemMap.put("goodNum", goodnum);
            itemMap.put("goodPrice", goodprice);
            itemMap.put("subtotal", new BigDecimal(goodprice).multiply(new BigDecimal(goodnum)));
            itemMap.put("storageOrderId", instorageOrderId);
            armyInstorageOrderDao.insertInstorageOrderItem(itemMap);

            //已经购买的产品数量 加上某个值
            itemMap.put("monthPlanId" , monthPlanId);
            armyMonthplanDao.decreaseArmyMonthPlanItem(itemMap);
            System.out.println(11);
        }
    }

    @Override
    public ArmyInstorageOrder getAmryInstorageOrderById(Long id) {
        return null;
    }

    public String getInstorageOrderCode() {
        Date currentDate = new Date();
        //编码前缀
        String prefix = "RK";
        String instorageDate = DateFormatUtils.format(currentDate, "yyyyMMdd");
        //查看当天有多少个数据 ，并在入库日期上加一
        Map param = new HashMap();
        param.put("instorageDate", instorageDate);
        Integer count = armyInstorageOrderDao.getCountInstorageOrder(param);
        return prefix + instorageDate + StringUtils.leftPad(String.valueOf(count + 1), 5, '0');
    }



}
